@extends('admin.base')
@section('title', '商品列表')
@section('content')
 <div class="layui-card">
    <fieldset class="layui-elem-field">
        <div class="layui-field-box">
            <span class="layui-breadcrumb" lay-separator=">>">
                <a class="layui-btn-sm" href="{{url('admin/shop/index')}}" style="cursor:pointer;" ><cite>商品列表</cite></a>;
            </span>
        </div>
    </fieldset>
    <div class="layui-card-header" style="height:100px">
      <div>
        <span class="pull-left">
          <div class="layui-form">
            <div class="layui-form-item">
              <div class="layui-inline">     
                <label class="layui-form-label">商品名</label>
                <div class="layui-input-inline" style="width:120px">
                    <input type="text" class="layui-input" id="sub_keywords" name="keywords" value="{{ isset($request['keywords']) ? $request['keywords'] : '' }}"  placeholder="请输入商品名">
                </div>
                   <label class="layui-form-label">商品ID</label>
                  <div class="layui-input-inline" style="width:120px">
                    <input type="text" class="layui-input" id="sub_id" name="sub_id" value="{{ isset($request['id']) ? $request['id'] : '' }}"  placeholder="请输入商品ID">
                </div>
                  <label class="layui-form-label">货源ID</label>
                <div class="layui-input-inline" style="width:120px">
                    <input type="text" class="layui-input" id="sub_gid" name="sub_gid" value="{{ isset($request['gid']) ? $request['gid'] : '' }}"  placeholder="请输入货源ID">
                </div>
                <label class="layui-form-label" style="width:90px">销售标签</label>
                <div class="layui-input-inline" style="width:100px">
                    <input type="text" class="layui-input" id="sub_title" name="sub_title" value="{{ isset($request['title']) ? $request['title'] : '' }}"  placeholder="请输入销售标题">
                </div>
                <label class="layui-form-label" style="width:60px">标签</label>
                <div class="layui-input-inline" style="width:150px">
                    <input type="text" class="layui-input" id="sub_label" name="sub_label" value="{{ isset($request['label']) ? $request['label'] : '' }}"  placeholder="请输入标签">
                </div>
                <label class="layui-form-label" style="width:60px">分类</label>
                <div class="layui-input-inline" style="width:130px">
                    <select name="sub_type" lay-verify="required">
                        <option value="0">请选择</option>
                        @foreach($all_list as $k=>$v)
                            <option value="{{$v->tid}}" @if($request['type'] == $v->tid) selected @endif><?php echo str_repeat("&nbsp;",substr_count($v->path,',')*3);?>|--{{$v->name}}</option>
                        @endforeach
                    </select>
                </div>
                <label class="layui-form-label" style="width:60px">品牌</label>
                <div class="layui-input-inline" style="width:130px">
                    <select name="sub_brand" lay-verify="required" lay-search="">
                        <option value="0">请选择</option>
                        @foreach($brand as $k=>$v)
                            <option value="{{$v->bid}}" @if($request['brand'] == $v->bid) selected @endif>{{$v->name}}</option>
                        @endforeach
                    </select>
                </div>
                <label class="layui-form-label" style="width:60px">状态</label>
                <div class="layui-input-inline" style="width:130px">
                    <select name="sub_state" lay-verify="required">
                        <option value="0">请选择</option>
                        <option value="1" @if($request->state == 1)selected @endif>上架</option>
                        <option value="2" @if($request->state == 2)selected @endif>下架</option>
                    </select>
                </div>
                <label class="layui-form-label" style="width:100px">是否专卖</label>
                <div class="layui-input-inline" style="width:130px">
                    <select name="sub_sell" lay-verify="required">
                        <option value="0">请选择</option>
                        <option value="1" @if($request->sell == 1)selected @endif>是</option>
                        <option value="2" @if($request->sell == 2)selected @endif>否</option>
                    </select>
                </div>
                  <label class="layui-form-label" style="width:100px">库存不足</label>
                  <div class="layui-input-inline" style="width:130px">
                    <select name="sub_num" lay-verify="required">
                        <option value="0">请选择</option>
                        <option value="1" @if($request->num == 1)selected @endif>是</option>
                        <option value="2" @if($request->num == 2)selected @endif>否</option>
                    </select>
                </div>
                  <label class="layui-form-label" style="width:100px;">排序</label>
                  <div class="layui-input-inline" style="width:130px;">
                    <select name="sub_sort" lay-verify="required">
                        <option value="3">请选择</option>
                        <option value="1" @if($request->sort == 1)selected @endif>时间正序</option>
                        <option value="2" @if($request->sort == 2)selected @endif>时间倒序</option>
                        <option value="3" @if($request->sort == 3)selected @endif>名称正序</option>

                    </select>
                </div>

                <label class="layui-form-label" style="display:none;">时间</label>
                <div class="layui-input-inline" style="display:none;">
                    <input  type="text" class="layui-input" class="jeinput" id="test11" placeholder="创建时间(年月日~年月日)" name="month" value="{{ isset($request['month']) ? $request['month'] : '' }}" readonly>
                </div>
                <div class="layui-input-inline">
                  <button class="layui-btn" lay-submit lay-filter="selectsub" style="margin-bottom:10px">搜索</button>
                     <button class="layui-btn layui-btn-danger" onclick="del_cy('{{url('admin/shop/dels')}}');" style="margin-bottom:10px;" >删除选中</button>
                  <!-- <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="exportsub" ></i>导出</button>  -->
                </div>
              </div>
            </div>
          </div>
        </span>
            
       <span class="pull-right">
            {{--<button class="layui-btn layui-btn-sm" onclick="active_cy('{{url('admin/shop/add')}}','','添加');">--}}
                {{--<i class="layui-icon">&#xe608;</i> 添加--}}
            {{--</button>--}}
        </span>
      </div>
    </div>

    <div class="layui-card-body">
        <style>
            table tr td.tc,table tr th.tc {
                text-align: center;
                cursor:default;
            }
        </style>
        <table class="layui-table" lay-even lay-size="">
            <colgroup>
                <col width="">
                <col width="">
                <col width="">
                <col width="">
                <col width="">
                <col width="">
            </colgroup>
            <thead>
                <tr>
                    <th class="tc">
                        <input type="checkbox"  name="" value="" id='ids' title="" lay-skin="primary" >
                    </th>

                    <th class="tc">商品ID</th>
                    <th class="tc">货源ID</th>
                    <th class="tc">商品名</th>
                    <th class="tc">销售标题</th>
                    <th class="tc">主图</th>
                    <th class="tc">其余主图</th>
                    <th class="tc">分类</th>
                    <th class="tc">品牌</th>
                    <th class="tc">标签</th>
                    <th class="tc">成本价(无规格)</th>
                    <th class="tc">销售价(无规格)</th>
                    <th class="tc">货源-销售价</th>
                    <th class="tc">销量</th>
                    <th class="tc">库存</th>
                    <th class="tc">排序</th>
                    <th class="tc">心愿单</th>

                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}
                    {{--<th class="tc">图片</th>--}}

                    <th class="tc">状态</th>
                    <th class="tc">是否专卖</th>
                    <th class="tc">创建时间</th>
                    <th class="tc">操作</th>
                </tr>
                <tbody>
                    @if($list->isEmpty())
                        <tr>
                            <td colspan="19" class="tc">暂无数据</td>
                        </tr>
                    @else
                        @foreach ($list as $v)
                            @if($v['s_state'] == 2)
                                <tr style="display:none">
                                @else
                                <tr>
                                @endif

                                <td><input type="checkbox"  name="ids" value="{{$v->id}}" title="" lay-skin="primary" ></td>
                                    <td class="tc">{{$v['id']}}</td>
                                    <td class="tc">{{$v['gid']}}</td>
                                <td class="tc">
                                    <a href="javascript:void(0);"
                                       onclick="sorts_name('{{$v->id}}','{{$v->name}}')">
                                        {{ $v['name'] }}</a>
                                </td>

                                <td class="tc">
                                    @if($v['title'])
                                        <a href="javascript:void(0);"
                                           onclick="sorts('{{$v->id}}','{{$v->title}}')">{{$v['title']}}</a>
                                    @else
                                        <a href="javascript:void(0);"
                                           onclick="sorts('{{$v->id}}','{{$v->value}}')">设置</a>
                                    @endif
                                </td>
                                <td class="tc" style='cursor:pointer' onclick="active('{{url('admin/shop/edit_mpic')}}','{{$v['id']}}','编辑主图')"><img src="{{$v['picname']}}" height="100px"></td>
                                <td class="tc"><a href="/admin/shop/pic_other?id={{$v['id']}}&page={{$request['page']}}&keywords={{$request['keywords']}}&label={{$request['label']}}&type={{$request['type']}}&brand={{$request['brand']}}&state={{$request['state']}}&sell={{$request['sell']}}&month={{$request['month']}}&title={{$request['title']}}">查看({{$v['other_num']}})</a></td>
                                <td class="tc">{{ $v['tname'] }}</td>
                                <td class="tc">{{ $v['bname'] }}</td>
                                <td class="tc"><a href="javascript:void(0);" onclick="active('{{url('admin/shop/label')}}','{{$v['id']}}','标签');" title="设置标签">{{ $v['label'] }}</a></td>
                                <td class="tc">{{ $v['cost_price'] }}</td>
                                <td class="tc">{{ $v['sell_price'] }}</td>
                                <td class="tc">{{ $v['sprice'] }}</td>
                                <td class="tc">{{ $v['sale_num'] }}</td>
                                <td class="tc">{{ $v['num'] }}</td>
                                <td class="tc"><a href="javascript:void(0);"
                                           onclick="sorts_paixu('{{$v->id}}','{{$v->sort}}')">{{$v->sort}}</a></td>
                                <td class="tc">
                                    <form class="layui-form " action="">
                                        @if($v['is_wish'] == 1)
                                            <input type="checkbox" checked name="" lay-skin="switch" lay-filter="switchCy" lay-text="开启|关闭" value="{{ $v['id'] }}">
                                        @else
                                            <input type="checkbox"  name="" lay-skin="switch" lay-filter="switchCy" lay-text="关闭|关闭" value="{{ $v['id'] }}">
                                        @endif

                                    </form>
                                </td>
                                {{--@foreach($v['img_arr'] as $val)--}}
                                    {{--@if(!empty($v['img_arr'][0]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][0]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][1]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][1]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][2]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][2]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][3]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][3]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][4]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][4]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][5]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][5]['picname']}}"></td>--}}
                                        {{--@else--}}
                                            {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][6]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][6]['picname']}}"></td>--}}
                                        {{--@else--}}
                                            {{--<td class="tc"></td>--}}
                                    {{--@endif--}}
                                    {{--@if(!empty($v['img_arr'][7]['picname']))--}}
                                        {{--<td class="tc"><img src="{{$v['img_arr'][7]['picname']}}"></td>--}}
                                    {{--@else--}}
                                        {{--<td class="tc"></td>--}}
                                    {{--@endif--}}

                                {{--@endforeach--}}
                                
                                <td class="tc">
                                    <form class="layui-form " action="">
                                        @if($v['state'] == 1)
                                            <input type="checkbox" checked name="" lay-skin="switch" lay-filter="switchTest" lay-text="在售|停售" value="{{ $v['id'] }}">
                                        @else
                                            @if($v['s_state'] == 2)
                                                <input type="checkbox"  name="" lay-skin="switch" lay-filter="switchTest" lay-text="在售|停售" value="{{ $v['id'] }}" disabled="disabled">
                                                @else
                                                <input type="checkbox"  name="" lay-skin="switch" lay-filter="switchTest" lay-text="在售|停售" value="{{ $v['id'] }}">
                                                @endif
                                        @endif

                                    </form>
                                </td>
                                <td class="tc">@if($v->is_sell == 1)
                                        <button class="layui-btn layui-btn-primary layui-btn-xs" title="推荐专区(点击更改为是)" onclick="config('/admin/shop/edit_state','{{$v->id}}','2','否')">
                                            是
                                        </button>
                                    @elseif($v->is_sell == 2)
                                        <button class="layui-btn layui-btn-danger layui-btn-xs" title="推荐专区(点击更改为否)" onclick="config('/admin/shop/edit_state','{{$v->id}}','1','是')">
                                            否
                                        </button>
                                    @endif
                                </td>
                                <td class="tc">{{$v['created_at']}}</td>
                                <td class="tc">
                                    <div class="layui-btn-group" >

                                            <button class="layui-btn layui-btn-sm" onclick="active_cy('{{url('admin/shop/source?id='.$v->id."&type=1&page=".$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}');" >素材库</button>
                                        <button class="layui-btn layui-btn-sm" onclick="active('{{url('admin/shop/source_add?page='.$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}','{{$v->id}}','添加','800px','600px');" >添加图文素材@if($v['pic_num'] != 1)<span style="color:#000">({{$v['pic_num']}})</span>@else <span>({{$v['pic_num']}})</span>@endif</button>
                                        <button class="layui-btn layui-btn-sm" onclick="active('{{url('admin/shop/source_add_vedio?page='.$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}','{{$v->id}}','添加','800px','600px');" >添加视频@if($v['vedio_num'] == 0)<span style="color:#000">({{$v['vedio_num']}})</span>@else <span>({{$v['vedio_num']}})</span>@endif</button>
                                        <button class="layui-btn layui-btn-sm" onclick="active('{{url('admin/shop/source_add_qanda?page='.$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}','{{$v->id}}','添加','800px','600px');" >添加问答@if($v['qanda_num'] == 0)<span style="color:#000">({{$v['qanda_num']}})</span>@else<span>({{$v['qanda_num']}})</span>@endif</button>
                                            {{--<button class="layui-btn layui-btn-sm" onclick="active_cy('{{url('admin/shop/qanda_add?page='.$request->page.'&keywords='.$request->keywords.'&id='.$v['id'].'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}')">问答库</button>--}}
                                        <button class="layui-btn layui-btn-sm" onclick="active_cy('{{url('admin/shop/edit?id='.$v->id.'&page='.$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}');">编辑</button>
                                        @if($v['is_attr'] == 1)
                                            <button class="layui-btn layui-btn-sm" style="color:white"><a href="{{url('admin/sku/index?id='.$v->id."&type=1&page=".$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort)}}" name="" style="color:white">选择规格</a></button>
                                        @else
                                        <button class="layui-btn layui-btn-sm  layui-btn-warm" style="color:white"><a href="{{url('admin/group/add?sku_id=0&id='.$v->id."&type=1&page=".$request->page.'&keywords='.$request->keywords.'&label='.$request->label.'&type='.$request->type.'&brand='.$request->brand.'&state='.$request->state.'&sell='.$request->sell.'&month='.$request->month.'&title='.$request->title.'&sort='.$request->sort.'&typeaa=1')}}" name="" style="color:white">设置拼团</a></button>
                                        @endif
                                        <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="del('{{url('admin/shop/del')}}','{{$v->id}}');" >删除</button>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                    @endif
                </tbody>
            </thead>
        </table>
        <!-- 分页 -->
        <div class="sabrosus" >{{ $page }}</div>
        <form>
            <div>
                第&nbsp;<input type="" name="page" value="{{$pages}}" style="width:50px;border:1px solid #ddd;line-height:70px;text-align:center;height:30px;border-radius:1px">&nbsp;页
                &nbsp;&nbsp;&nbsp;&nbsp;<button class="layui-btn layui-btn-sm" lay-submit lay-filter="selectsub">跳转</button>
            </div>
        </form>
        <!-- {{$list->total()}} -->
    </div>

 </div>
@endsection
@section('js')
    <script>
        // 删除按钮事件 -- 弹出删除层
        var config = function(url,id,state,title){
            layer.confirm('确定更改('+title+')！', {
                btn: ['确定','取消'] //按钮
            },function(){
                $.post(url,{
                    id: id,state:state,
                },function(data){
                    console.log(data);
                    layer.msg(data.msg);
                    // if(data.code == 1){
                    //     layer.msg('更改成功！');
                    // }else{
                    //     layer.msg('更改失败！');
                    // }
                    location.href = location.href;
                });
            });
        }

        $("#ids").click(function(){
            var val = $("#ids").prop('checked');
            if(val == true){
                $("input[type=checkbox]").attr('checked',true);
            }else{
                $("input[type=checkbox]").attr('checked',false);
            }
        })

        layui.use('form', function(){
            var form = layui.form;
            form.render();

            //搜索
            form.on('submit(selectsub)', function(data){
                let keywords = $('#sub_keywords').val();//关键字
                let month = $("#test11").val();//时间
                var sub_label = $("input[name=sub_label]").val();
                var sub_type = $("select[name=sub_type]").val();
                var sub_brand = $("select[name=sub_brand]").val();
                var sub_state = $("select[name=sub_state]").val();
                var sub_sell = $("select[name=sub_sell]").val();
                var sub_title = $("input[name=sub_title]").val();
                var sub_sort = $("select[name=sub_sort]").val();
                var sub_gid = $("input[name=sub_gid]").val();
                var sub_id = $("input[name=sub_id]").val();
                let page = $("input[name=page]").val();
                var sub_num = $("select[name=sub_num]").val();
                location.href = "{{url('admin/shop/index')}}"+"?keywords=" + keywords + "&month=" + month
                    + '&page=' + page
                    + '&label='+ sub_label
                    + '&type=' + sub_type
                    + '&brand=' + sub_brand
                    + '&state=' + sub_state
                    + '&title=' + sub_title
                    + '&sell=' + sub_sell
                    + '&sort=' +sub_sort
                    + '&gid=' +sub_gid
                    + '&id=' + sub_id
                    +'&num=' + sub_num;
                return false;
            });

            //监听指定开关
            form.on('switch(switchTest)', function(data){
                var id = data.value;

                if(data.elem.checked){
                    var state = 1;//启用
                }else{
                    var state = 2;//禁用               
                }
                ajax('/admin/shop/up_state',{id:id,state:state});
            });

            //监听指定开关
            form.on('switch(switchCy)', function(data){
                var id = data.value;

                if(data.elem.checked){
                    var is_wish = 1;//启用
                }else{
                    var is_wish = 2;//禁用               
                }
                ajax('/admin/shop/up_wish',{id:id,is_wish:is_wish});
            });
        });
        function active_cy(url){
            location.href=url;
        }

        function sorts(id, title) {
            layer.prompt({
                formType: 2,
                value: title,
                title: '编辑'
            }, function (value, index, elem) {
                var index = layer.load();
                $.post("/admin/shop/update_title", {
                    id: id,
                    title: value,
                    _token: "{{csrf_token()}}"
                }, function (data) {
                    layer.close(index);
                    layer.msg(data.msg);
                    if (data.code == 1) {
                        location.reload();
                    }

                })
            });
        }

        function sorts_name(id, name) {
            layer.prompt({
                formType: 2,
                value: name,
                title: '编辑'
            }, function (value, index, elem) {
                var index = layer.load();
                $.post("/admin/shop/update_name", {
                    id: id,
                    name: value,
                    _token: "{{csrf_token()}}"
                }, function (data) {
                    layer.close(index);
                    layer.msg(data.msg);
                    if (data.code == 1) {
                        location.reload();
                    }

                })
            });
        }

        function sorts_paixu(id,paixu){
            layer.prompt({
                formType: 3,
                value: paixu,
                title: '编辑'
            }, function (value, index, elem) {
                var index = layer.load();
                $.post("/admin/shop/sorts_paixu", {
                    id: id,
                    paixu: value,
                    _token: "{{csrf_token()}}"
                }, function (data) {
                    layer.close(index);
                    layer.msg(data.msg);
                    if (data.code == 1) {
                        location.reload();
                    }

                })
            });
        }

        function del_cy(){
            var ids = $("input[name='ids']:checked");
            var ids_cy = [];
            for(let i =0 ;i < ids.length;i++){
                if(ids[i].checked){
                    ids_cy.push(ids[i].value)
                }
            }
            if(ids_cy.length <= 0){
                layer.msg('请选中需要删除的商品');
                return false;
            }
            layer.confirm('确定删除！', {
                btn: ['确定','取消'] //按钮
            },function(){
                $.post("/admin/shop/del_ids", {
                    id: ids_cy,
                    _token: "{{csrf_token()}}"
                }, function (data) {
                    layer.msg(data.msg);
                    if (data.code == 1) {
                        location.reload();
                    }

                })
            })
        }
    </script>
@endsection